<?php
namespace app\warehouse\controller;

use think\Db; 
class Console extends Base{

	public function category(){
        return view('category');
    }

    //分类列表
	public function category_json(){
        $arr = $this->api_category_list();
        //var_dump($data);
        $data['code'] = 0;
        $data['count'] = count($arr);
        $data['data'] = $arr;
        echo json_encode($data);
    }
    //编辑分类页面
    public function category_edit($id){
        $arr = $this->api_category_list();
        if($id > 0){
            $info = Db::table('warehouse_category')->find($id);
        }else{//新增数据
            $info['id'] = 0;
            $info['name'] = "";
            $info['sort'] = 1;
            $info['description'] = "";
            $info['parent_id'] = 0;
        }
        
        $this->assign('arr',$arr);
        $this->assign('info',$info);
        return view('category_edit');
    }
    //保存编辑的分类数据
    public function category_save(){
        $id = input("post.id");
        $data['name'] = input("post.name");
        $data['parent_id'] = input("post.parent_id");
        $data['description'] = input("post.description");
        $data['sort'] = input("post.sort");
        if($id == 0){
            Db::table('warehouse_category')->insert($data);//新增
        }else{
            Db::table('warehouse_category')->where('id', $id)->update($data);//更新
        }
        //返回成功
        $ret['status'] = 1;
        echo json_encode($ret);
    }
    //删除分类
    public function category_delete(){
        $id = input("post.id");
        $cnum = Db::table('warehouse_category')->where('parent_id', $id)->count();//下面是否有分类
        $inum = Db::table('warehouse_item')->where('cid', $id)->count();//是否有物料
        if($cnum == 0 && $inum == 0){
            Db::table('warehouse_category')->where('id', $id)->delete();
            $ret['status'] = 1;
        }else{
            $ret['status'] = 0;
        }
        echo json_encode($ret);
    }
	public function brand(){
        return view('brand');
    }

	public function supplier(){
        return view('supplier');
    }
    
}